const Koa = require('koa')
const cors = require('koa2-cors')
const app = new Koa()
const debug = require('debug')('koa-weapp-demo')
const { deleteLogSchedule } = require('./tools/schedule')
const log = require('./middlewares/log')
const response = require('./middlewares/response')
const xmlParser = require('koa-xml-body')
const bodyParser = require('./middlewares/bodyparser')
const config = require('./config')

// 定时器任务 -- 删除日志
deleteLogSchedule()

// 日志打印
app.use(log)

// 跨域
app.use(cors())

// 使用响应处理中间件
app.use(response)

// 解析 xml
app.use(xmlParser())

// 解析请求体
app.use(bodyParser())

// 引入路由分发
const router = require('./routes')
app.use(router.routes())

// 启动程序，监听端口
app.listen(config.port, () => debug(`listening on port ${config.port}`))